home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / mm-0.90 / getmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  2.7 KB  |  135 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  * the City of New York.  Permission is granted to any individual or
  4.  * institution to use, copy, or redistribute this software so long as it
  5.  * is not sold for profit, provided this copyright notice is retained.
  6.  */
  7.  
  8. #include "config.h"
  9. #include "osfiles.h"
  10. #include "compat.h"
  11.  
  12. char *progname;
  13. int locked = 0;
  14. char lockname[MAXPATHLEN];
  15.  
  16. main (argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     char *from, *to, *cp;
  21.     char destdir[MAXPATHLEN];
  22.     struct stat st;
  23.     int c, ifd, ofd;
  24.     time_t mtime;
  25.     char *rindex ();
  26.  
  27.     if (argc != 3) {
  28.     fprintf (stderr, "usage: %s source-file destination-file\n", argv[0]);
  29.     exit (1);
  30.     }
  31.  
  32.     progname = (cp = rindex (argv[0], '/')) ? cp + 1 : argv[0];
  33.  
  34.     from = argv[1];
  35.     to = argv[2];
  36.  
  37.     if (cp = rindex (to, '/')) {
  38.     strcpy (destdir, to);
  39.     destdir[cp - to] = 0;
  40.     }
  41.     else
  42.     strcpy (destdir, ".");
  43.  
  44.     if (access (from, R_OK) != 0)
  45.     fatal ("cannot access", from);
  46.  
  47.     if (access (to, F_OK) == 0) {
  48.     fprintf (stderr, "%s: will not overwrite %s\n", progname, to);
  49.     exit (1);
  50.     }
  51.  
  52.     if (access (destdir, W_OK|X_OK) != 0)
  53.     fatal ("cannot write", destdir);
  54.  
  55. #ifdef MAIL_USE_FLOCK
  56.     if ((ifd = open (from, O_RDONLY)) < 0)
  57.     fatal ("cannot open", to);
  58.  
  59.     if (flock (ifd, LOCK_EX|LOCK_NB) != 0)
  60.     fatal ("cannot lock", from);
  61. #else
  62.     sprintf (lockname, "%s.lock", from);
  63.     if (creat (lockname, 0) < 0)
  64.     fatal ("cannot lock", from);
  65.     locked = 1;
  66.     if ((ifd = open (from, O_RDONLY)) < 0)
  67.     fatal ("cannot open", to);
  68. #endif
  69.  
  70.     if (stat (from, &st) < 0)
  71.     fatal ("cannot stat", from);
  72.     mtime = st.st_mtime;
  73.  
  74.     if ((ofd = open (to, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0)
  75.     fatal ("cannot open", to);
  76.  
  77.     for (;;) {
  78.     char buf[BUFSIZ];
  79.     c = read (ifd, buf, sizeof buf);
  80.     if (c == 0)
  81.         break;
  82.     if (c < 0)
  83.         fatal ("error reading", from);
  84.     if (write (ofd, buf, c) < c)
  85.         fatal ("error writing", to);
  86.     }
  87.  
  88.     if (close (ofd) < 0)
  89.     fatal ("error closing", to);
  90.     
  91.     if (stat (from, &st) < 0)
  92.     fatal ("cannot stat", from);
  93.  
  94.     if (st.st_mtime != mtime)
  95.     fprintf (stderr, "%s: %s changed!\n", progname, from);
  96.  
  97.     /*
  98.      * Try to unlink the source file.  If that
  99.      * fails, just truncate it.
  100.      */
  101.     if (unlink (from) < 0) {
  102.     if (stat (from, F_OK) == 0)
  103.         if ((c = creat (from, 0660)) < 0)
  104.         fatal ("cannot truncate", from);
  105.         else
  106.         (void) close (c);
  107.     }
  108.     done (0);
  109.  
  110. }
  111.  
  112. fatal (s1, s2)
  113. char *s1, *s2;
  114. {
  115.     if (s2) {
  116.     int saved_errno = errno;
  117.     fprintf (stderr, "%s: %s ", progname, s1);
  118.     errno = saved_errno;
  119.     perror (s2);
  120.     }
  121.     else
  122.     fprintf (stderr, "%s: %s\n", progname, s1);
  123.     done (1);
  124. }
  125.  
  126. done (n)
  127. int n;
  128. {
  129. #ifndef MAIL_USE_FLOCK
  130.     if (locked)
  131.     unlink (lockname);
  132. #endif
  133.     exit (n);
  134. }
  135.